﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Kmust.App_Data.KmustDataTableAdapters;
using Kmust.Commom;
using System.Data;

namespace Kmust.KmustControl
{
    /// <summary>
    /// OrganizationControl.xaml 的交互逻辑
    /// </summary>
    public partial class OrganizationControl : UserControl
    {
        private List<OrganizationNodeItem> listNode;
        /// <summary>
        /// WPFCompanyTree 选择的列改变引发的事件
        /// </summary>
        public event Action<object, OrganizationNodeItem> OrganizationControlSelectedItemChanged;
        /// <summary>
        /// 申明委托
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate void EventCodeHandler(object send,object e);

        private List<OrganizationNodeItem> listExpandNode;
        public string OrganizationCode;
        public OrganizationControl()
        {
            InitializeComponent();
            this.Organization_Tree.SelectedItemChanged += Organization_Tree_SelectedItemChanged;
            InitDbData();
            listExpandNode = InitTreeData();
            Organization_Tree.ItemsSource = listExpandNode;
            ExpandInternal(listExpandNode, 1);
        }

        private void Organization_Tree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (OrganizationControlSelectedItemChanged != null)
                OrganizationControlSelectedItemChanged(sender, Organization_Tree.SelectedItem as OrganizationNodeItem);
            //MessageBox.Show(Organization_Tree.SelectedItem.ToString().);
        }
        private void InitDbData()
        {
            DataTable orgdt;
            using (OrganizationTableAdapter org = new App_Data.KmustDataTableAdapters.OrganizationTableAdapter())
            {
                orgdt = org.GetData();
            }
            int count = orgdt.Rows.Count;
            listNode = new List<OrganizationNodeItem>();
            for (int i = 0; i < count; i++)
            {
                listNode.Add(new OrganizationNodeItem() { Id = Convert.ToString(orgdt.Rows[i][4]), Code = Convert.ToString(orgdt.Rows[i][4]), Name = orgdt.Rows[i][6].ToString(), ParentCode = Convert.ToString(orgdt.Rows[i][1]) });
            }
        }
        private List<OrganizationNodeItem> InitTreeData()
        {
            List<OrganizationNodeItem> nodes = new List<OrganizationNodeItem>();
            listNode.ForEach(s =>
            {
                if(s.ParentCode=="")
                {
                    nodes.Add(s);
                    NextFindNode(s, s.Code);
                }
                else
                {
                    NextFindNode(s, s.Code);
                }
            });
          
            return nodes;
        }

        private void NextFindNode(OrganizationNodeItem mynode, string code)
        {
            foreach (OrganizationNodeItem node in listNode)
            {
                if (node.ParentCode == code)
                {
                    mynode.Children.Add(node);
                }
            }
          
        }
        /// <summary>
        /// 展开树节点到某一级
        /// </summary>
        /// <param name="list">构建树的所有节点集合</param>
        /// <param name="lev"></param>
        private void ExpandInternal(List<OrganizationNodeItem> list, int lev)
        {
            if (lev > 3)  //展开到第三层
                return;
            list.ForEach(s => ExpandInternal(s.Children, lev + 1));
            list.ForEach(s => s.Expand = true);
        }

        public event EventCodeHandler EventCode;



        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
           // MessageBox.Show(this.Organization_Tree.ItemTemplateSelector);
            string s=  (e.OriginalSource as CheckBox).Uid.ToString();
            MessageBox.Show(s);
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            string s = (e.OriginalSource as CheckBox).Uid.ToString();
         //   MessageBox.Show(s);
            if (s != null)
            {
                EventCode(this,(object)s);
            }
        }      
    }
}
